package com.ruoyi.web.controller.wb;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.OrderSummaryVo;
import com.ruoyi.wb.domain.WbRepairOrder;
import com.ruoyi.wb.enmus.WbRepairApplyOrderType;
import com.ruoyi.wb.mapper.WbRepairOrderMapper;
import com.ruoyi.wb.service.IWbRepairOrderService;
import com.ruoyi.wb.vo.WbRepairOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 维修订单Controller
 *
 * @author ruoyi
 * @date 2024-10-14
 */
@Api(tags="维修订单信息管理")
@RestController
@RequestMapping("/wb/wbRepairOrder")
public class WbRepairOrderController extends BaseController {
    @Autowired
    private IWbRepairOrderService wbRepairOrderService;

    @Autowired
    private WbRepairOrderMapper wbRepairOrderMapper;
    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);
    /**
     * 查询维修订单列表
     */
    @ApiOperation("查询维修订单列表")
    @GetMapping("/list")
    public TableDataInfo list(WbRepairOrder wbRepairOrder) {
        startPage();
        List<WbRepairOrderVo> list = wbRepairOrderService.selectWbRepairOrderList(wbRepairOrder);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(wbRepairOrderService.selectWbRepairOrderListCount(wbRepairOrder));
        return dataTable;
    }

    /**
     * 查询维修订单列表小程序
     */
    @ApiOperation("查询维修订单列表小程序")
    @GetMapping("/xcxList")
    public TableDataInfo xcxList(WbRepairOrder wbRepairOrder) {
        startPage();
        List<WbRepairOrderVo> list = wbRepairOrderService.selectWbRepairOrderList(wbRepairOrder);
        Long userId = getUserId();
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(wbRepairOrderService.selectWbRepairOrderListCount(wbRepairOrder));
        if (!CollectionUtils.isEmpty(list)){
            List<WbRepairOrderVo> collect = list.stream().filter(item -> userId.equals(item.getRepairerId())).collect(Collectors.toList());
            TableDataInfo dataTable2 = getDataTable(collect);
            dataTable2.setTotal(wbRepairOrderService.selectWbRepairOrderListCount(wbRepairOrder)-(list.size()-collect.size()));
            return dataTable2;
        }
        return dataTable;
    }



    /**
     * 查询维修订单列表
     */
    @ApiOperation("查询维保公司订单列表")
    @GetMapping("/wbList")
    public TableDataInfo wbList(WbRepairOrder wbRepairOrder) {
        startPage();
        List<WbRepairOrderVo> list = wbRepairOrderService.selectWbRepairOrderWbList(wbRepairOrder);
        TableDataInfo dataTable = getDataTable(list);
        dataTable.setTotal(wbRepairOrderService.selectWbRepairOrderWbListCount(wbRepairOrder));
        return dataTable;
    }

    /**
     * 查询维修订单列表
     */
    @ApiOperation("查询维保公司订单列表")
    @GetMapping("/wbListCount")
    public AjaxResult wbListCount(WbRepairOrder wbRepairOrder) {
        List<WbRepairOrderVo> list = wbRepairOrderService.selectWbRepairOrderWbList(wbRepairOrder);
        List<WbRepairOrderVo> collect = list.stream().filter(item -> item.getOrderStatus() <= WbRepairApplyOrderType.UNDERWAY.getCode()).collect(Collectors.toList());
        return success(collect.size());
    }

    /**
     * 导出维修订单列表
     */
    @ApiOperation("导出维修订单列表")
    @Log(title = "维修订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WbRepairOrder wbRepairOrder) {
        List<WbRepairOrder> list = BeanUtil.copyToList(wbRepairOrderService.selectWbRepairOrderList(wbRepairOrder),WbRepairOrder.class);
        ExcelUtil<WbRepairOrder> util = new ExcelUtil<WbRepairOrder>(WbRepairOrder.class);
        util.exportExcel(response, list, "维修订单数据");
    }

    /**
     * 获取维修订单详细信息
     */
    @ApiOperation("获取维修订单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(wbRepairOrderService.selectWbRepairOrderById(id));
    }

    /**
     * 新增维修订单
     */
    @ApiOperation("新增维修订单")
    @Log(title = "维修订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WbRepairOrder wbRepairOrder) {
        return toAjax(wbRepairOrderService.insertWbRepairOrder(wbRepairOrder));
    }

    /**
     * 修改维修订单
     */
    @ApiOperation("修改维修订单")
    @Log(title = "维修订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WbRepairOrder wbRepairOrder) {
        return toAjax(wbRepairOrderService.updateWbRepairOrder(wbRepairOrder));
    }

    /**
     * 删除维修订单
     */
    @ApiOperation("删除维修订单")
    @Log(title = "维修订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(wbRepairOrderService.deleteWbRepairOrderByIds(ids));
    }



    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/appWxPayOrder")
    @ApiOperation("下单")
    public AjaxResult appPayOrder(@RequestBody WbRepairOrder wbRepairOrder)
    {
        try {
            return  success(wbRepairOrderService.wxPayOrder(wbRepairOrder));
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
    }


    /**
     * 统计当前维修师傅订单
     * @return {@link OrderSummaryVo}
     */
    @GetMapping("/statisticsByUser")
    public AjaxResult statisticsByUser( ) {
        return success(wbRepairOrderService.statisticsByUser());
    }

    /**
     * 微信支付回调回调
     */
    @PostMapping("/payNotify")
    public AjaxResult payNotify(HttpServletRequest request, HttpServletResponse response)
    {
        //注意：回调接口需要暴露到公网上，且要放开token验证
        log.info("+++++++进入了回调++++++++++++"+ response);
        Map<String, Object> map = wbRepairOrderService.payNotify(request, response);
        return success(map);

    }
}